package cn.e2win.base.json.vo;

import java.io.Serializable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import cn.e2win.base.entity.SysUser;
import cn.e2win.biz.security.UserSession;
import cn.e2win.biz.util.DateUtils;
import cn.e2win.biz.util.StringUtils;

public class Pager implements Serializable {
	
	private SysUser currUser;
	
	private boolean returnAll = false;
	public static final String DEF_ORDER_BY = " ORDER BY 1 DESC";
	
	
	//忽略
	public static final String _I = "_I";
	//IN
	public static final String _IN = "_IN";
	//<>
	public static final String _N = "_N";
	//LIKE
	public static final String _L = "_L";
	//NOT LIKE
	public static final String _NL = "_NL";
	//DATE_BEGIN >=
	public static final String _B = "_B";
	//DATE_BEGIN >
	public static final String _NE = "_NE";
	//DATE_END
	public static final String _E = "_E";
	//DATE_END <
	public static final String _NB = "_NB";
	//IS NULL
	public static final String _NULL = "_NULL"; 
	//IS NOT NULL
	//public static final String _NNULL = "_NNULL";
	
	private Map<String, String> scMap = new HashMap<String, String>();
	private Map dbMap = new HashMap<String, String>();
	private List<Map<String, Object>> resultList;
	private int totalCount;
	private int totalPage;
	private int pageNum = 1;
	private int numPerPage = 15;
	private String action;
	private String sql;
	private String orderField = null;
	private String orderDirection = null;
	{
		if(UserSession.getUser()!=null){
			if(UserSession.getUser().getNumPerPage()!=null){
				numPerPage = UserSession.getUser().getNumPerPage();
			}
			//scMap.put("coId", UserSession.getUser().getCoId()+"");
		}		
	}
	
	
	
	public SysUser getCurrUser() {
		return currUser;
	}
	public void setCurrUser(SysUser currUser) {
		this.currUser = currUser;
	}
	public boolean isReturnAll() {
		return returnAll;
	}
	public void setReturnAll(boolean returnAll) {
		this.returnAll = returnAll;
	}
	public Map<String, String> getScMap() {
		return scMap;
	}
	public void setScMap(Map<String, String> scMap) {
		this.scMap = scMap;
	}
	
	public Map getDbMap() {
		return dbMap;
	}
	public List<Map<String, Object>> getResultList() {
		return resultList;
	}
	public void setResultList(List<Map<String, Object>> resultList) {
		this.resultList = resultList;
	}
	public int getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	public int getTotalPage() {
		return totalPage;
	}
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getPageNum() {
		return pageNum;
	}
	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}
	public int getNumPerPage() {
		return numPerPage;
	}
	public void setNumPerPage(int numPerPage) {
		this.numPerPage = numPerPage;
	}
	public String getSql() {
		return sql;
	}
	public void processSql(String sql){
		//dbMap = CloneUtils.clone(scMap);
		String returnSql = "";

		for(Iterator<Entry<String, String>> it=scMap.entrySet().iterator();it.hasNext();){
			Entry<String, String> entry = it.next();
			if(!StringUtils.isEmpty(entry.getValue())){
				returnSql = returnSql + getOperator(entry.getKey());
			}
		}
		if(!"".equals(returnSql)){
			sql = sql.replaceAll("1=1", "1=1 " +returnSql);
		}
		if(!StringUtils.isEmpty(this.getOrderField())){
			String orderBy ="";
			orderBy = " ORDER BY " + this.getOrderField();
			if(!StringUtils.isEmpty(this.getOrderDirection())){
				orderBy = orderBy + " " +this.getOrderDirection();
			}
			if(sql.indexOf("ORDER BY 1 DESC,")>0){
				sql = sql.replaceAll("ORDER BY 1 DESC,", orderBy + ", ");
			}else if(sql.indexOf("ORDER BY 1 DESC")>0){
				sql = sql.replaceAll("ORDER BY 1 DESC", orderBy);
			}else if(sql.indexOf("ORDER BY 1 ASC,")>0){
				sql = sql.replaceAll("ORDER BY 1 ASC,", orderBy + ", ");
			}else if(sql.indexOf("ORDER BY 1 ASC")>0){
				sql = sql.replaceAll("ORDER BY 1 ASC", orderBy);
			}else if(sql.indexOf("ORDER BY 1,")>0){
				sql = sql.replaceAll("ORDER BY 1,", orderBy + ",");
			}else if(sql.indexOf("ORDER BY 1")>0){
				sql = sql.replaceAll("ORDER BY 1", orderBy);
			}
		}
			
			
			
		this.sql = sql;
	}
	
	private String getOperator (String field){
		if(field.endsWith(_I)){
			return "";
		}else{
			String key = field;
			String oper = " = :";
			if(field.endsWith(_L)){
				key = field.substring(0, field.length() - _L.length());
				oper = " LIKE :";
				dbMap.put(field, "%" + scMap.get(field) + "%");
			}else if(field.endsWith(_IN)){
				key = field.substring(0, field.length() - _IN.length());
				oper = " IN (:"+field+")";
				dbMap.put(field+"", Arrays.asList(scMap.get(field).split("【】")));
				field="";
			}else if(field.endsWith(_B)){
				key = field.substring(0, field.length() - _B.length());
				oper = " >= :";
				if(field.endsWith("Date" + _B)){
					dbMap.put(field, DateUtils.str2Date((String)scMap.get(field)));
				}else if(field.endsWith("id" + _B) || field.endsWith("Id" + _B) || field.endsWith("Int" + _B)){
					dbMap.put(field, Integer.parseInt((String)scMap.get(field)));				
				}else if(field.endsWith("Float" + _B)){
					dbMap.put(field, Float.parseFloat((String)scMap.get(field)));				
				}else{
					dbMap.put(field, scMap.get(field));	
				}	
			}else if(field.endsWith(_NE)){
				key = field.substring(0, field.length() - _NE.length());
				oper = " > :";
				if(field.endsWith("Date" + _NE)){
					dbMap.put(field, DateUtils.str2Date((String)scMap.get(field)));
				}else if(field.endsWith("id" + _NE) || field.endsWith("Id" + _NE) || field.endsWith("Int" + _NE)){
					dbMap.put(field, Integer.parseInt((String)scMap.get(field)));				
				}else if(field.endsWith("Float" + _NE)){
					dbMap.put(field, Float.parseFloat((String)scMap.get(field)));				
				}else{
					dbMap.put(field, scMap.get(field));	
				}	
			}else if(field.endsWith(_NB)){
				key = field.substring(0, field.length() - _NB.length());
				oper = " < :";
				if(field.endsWith("Date" + _NB)){
					dbMap.put(field, DateUtils.getOffsetDay(DateUtils.str2Date((String)scMap.get(field)), 1));
				}else if(field.endsWith("id" + _NB) || field.endsWith("Id" + _NB) || field.endsWith("Int" + _NB)){
					dbMap.put(field, Integer.parseInt((String)scMap.get(field)));				
				}else if(field.endsWith("Float" + _NB)){
					dbMap.put(field, Float.parseFloat((String)scMap.get(field)));				
				}else{
					dbMap.put(field, scMap.get(field));	
				}		
			}else if(field.endsWith(_E)){
				key = field.substring(0, field.length() - _E.length());
				oper = " <= :";
				if(field.endsWith("Date" + _E)){
					dbMap.put(field, DateUtils.getOffsetDay(DateUtils.str2Date((String)scMap.get(field)), 1));
				}else if(field.endsWith("id" + _E) || field.endsWith("Id" + _E) || field.endsWith("Int" + _E)){
					dbMap.put(field, Integer.parseInt((String)scMap.get(field)));				
				}else if(field.endsWith("Float" + _E)){
					dbMap.put(field, Float.parseFloat((String)scMap.get(field)));				
				}else{
					dbMap.put(field, scMap.get(field));	
				}	
			}else if(field.endsWith(_N)){
				key = field.substring(0, field.length() - _N.length());
				oper = " <> :";
//以下代码会将field 设为 空字符串，所以必须放在最后。
			} else if(field.endsWith(_NULL)){
				key = field.substring(0, field.length() - _NULL.length());
				oper = " IS ";
				field = (String)scMap.get(field);
			} /*else if(field.endsWith(_NNULL)){
				key = field.substring(0, field.length() - _NNULL.length());
				oper = " IS NOT NULL ";
				field = "";
			} */
			else{
				key = field;
				oper = " = :";
				dbMap.put(field, scMap.get(field));	
			}
			return " AND " + key + oper + field;
		}
	}
	public String getAction() {
		return action;
	}
	public void setAction(String action) {
		this.action = action;
	}
	public String getOrderField() {
		return orderField;
	}
	public void setOrderField(String orderField) {
		this.orderField = orderField;
	}
	public String getOrderDirection() {
		return orderDirection;
	}
	public void setOrderDirection(String orderDirection) {
		this.orderDirection = orderDirection;
	}
	
	
	
	
}
